// releasePage/pages/addHirecar/addHirecar.js
import throttle from '../../../utils/throttle.js';
import Http from '../../../utils/http.js';
const http = new Http();

const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        ipxBottom: 0,
        unHandleCnt: 0, //未处理数量
        allCnt: '',
        sceneImageArr: [], //上传照片
        description: '',
    },

    adviceListTap(e) {
        wx.navigateTo({
            url: `../../../functionPage/pages/news/adviceList/adviceList`,
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (app.globalData.isIpx) {
            this.setData({
                ipxBottom: 34,
            });
        }
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        // if (
        //     app.globalData.authority == '00' ||
        //     app.globalData.authority == '01'
        // ) {
        // }
        this.getUnHandleCnt();
    },

    description(e) {
        this.setData({
            description: e.detail.value,
        });
        app.numLimitTip(e.detail.value.length, 200);
    },
    // 添加图片
    addImg(e) {
        const that = this;
        wx.chooseImage({
            count: 8 - that.data.sceneImageArr.length,
            success(res) {
                let imgLen =
                    that.data.sceneImageArr.length + res.tempFilePaths.length;
                if (imgLen > 8) {
                    wx.showModal({
                        content: '最多上传8张图片，请重新上传',
                        showCancel: false,
                    });
                    return;
                } else {
                    wx.showLoading({
                        title: '图片上传中',
                        mask: true,
                    });
                    const imgUploadResults = [];
                    let index = that.data.sceneImageArr.length;
                    for (let i = 0; i < res.tempFilePaths.length; i++) {
                        let imgUploadResult = app
                            .imgUpload(res.tempFilePaths[i])
                            .then((res) => {
                                let num = i + index;
                                that.data.sceneImageArr[num] = res;
                                that.setData({
                                    sceneImageArr: that.data.sceneImageArr,
                                });
                                console.log(that.data.sceneImageArr);
                            });
                        imgUploadResults.push(imgUploadResult);
                    }
                    Promise.all(imgUploadResults).then((values) => {
                        wx.hideLoading();
                    });
                }
            },
        });
    },
    // 删除图片
    deleteImg(e) {
        let index = e.currentTarget.dataset.index;
        console.log(index);
        this.data.sceneImageArr.splice(index, 1);
        this.setData({
            sceneImageArr: this.data.sceneImageArr,
        });
        console.log(this.data.sceneImageArr);
    },
    // 预览图片
    previewImage(e) {
        const current = e.target.dataset.src;
        wx.previewImage({
            current,
            urls: this.data.sceneImageArr,
        });
    },

    submit: throttle(
        function (e) {
            let _that = this;
            if (app.visitorPublish()) return; //游客发布限制
            if (_that.data.description.length === 0) return;
            let data = {
                picurl: JSON.stringify(_that.data.sceneImageArr),
                content: _that.data.description,
            };
            http.postRequest(
                `cjMap/createSysProposal?authId=${app.globalData.authId}`,
                data
            ).then((res) => {
                if (res.status == 0) {
                    _that.setData({
                        description: '',
                    });
                    wx.showToast({
                        title: '提交成功',
                        icon: 'success',
                        duration: 2000,
                    });
                    if (
                        app.globalData.authority == '00' ||
                        app.globalData.authority == '01'
                    ) {
                        _that.getUnHandleCnt();
                    }
                    setTimeout(function () {
                        wx.navigateBack({
                            delta: 1, // 返回上一级页面。
                        });
                    }, 1000);
                }
            });
        },
        4000,
        {
            trailing: false,
        }
    ),
    //查询待处理数量
    getUnHandleCnt() {
        let _that = this;
        //经验跳转
        http.getRequest(
            `cjMap/getUnHandleCnt?authId=${app.globalData.authId}`
        ).then((res) => {
            if (res.status == 0) {
                if (
                    app.globalData.authority == '00' ||
                    app.globalData.authority == '01'
                ) {
                    let pages = getCurrentPages(); //当前页面栈
                    let prevPage = pages[pages.length - 2];
                    prevPage.setData({
                        unHandleCnt: res.unHandleCnt,
                    });
                    _that.setData({
                        unHandleCnt: res.unHandleCnt,
                        allCnt: res.allCnt,
                    });
                }
            }
        });
    },
});
